
// This function handles synchronizing of the information such as
// new messages, contact updates, etc.

var ChatboxLoop = new Class({
	// We have nothing to initialize, because the loop is started manually.
	initialize: function()
	{
		// :-)
		
	}, // end function initialize
	
	// This function starts the loop, or beter to say: It executes one cycle again.
	start: function()
	{
		// The user needs to be logged in for this.
		if (chat.getClsSession().getStatus() == -1)
			return;
		
		// Request the loop
		chat.getClsAjaxReq().request('loop', null, null, function(t)
		{
			// Maybe the user killed his session just while loading the loop page.
			// So we don't need to process the new information.
			if (chat.getClsSession().isKilled())
				return;
			
			// Execute the data we got.
			chat.getChat().getClsLoop().Execute(t);
			
		}, null, 2);
		
	}, // end function start
	
	// Parse a contact. This function is a bridge between the data gotten,
	// and the contacts class.
	parseContact: function(data)
	{
		// The user needs to be logged in for this.
		if (chat.getClsSession().getStatus() == -1)
			return;
		
		// Syntax: contact,field,new_value
		var data = data.split(",");
		var contact = data[0];
		var field   = data[1];
		var value   = "";
		
		// The value might contain ,'s on it self too! So we need to check that.
		if (data.length > 3)
		{
			// Yep, the value contains ,'s. So get every element after that.
			for (var i = 2; i < data.length; i++)
				// Don't add an komma when it's the first element we're parsing.
				if (i == 2)
					value += data[i];
				else
					value += "," + data[i];
		} // end if
		else
			// Nop, just plain simple.
			value = data[2];
		
		// Update the contacts list.
		chat.getChat().getClsContacts().setChanges(contact, field, value);
		
		//alert('Altering the field ' + field + ' of contact ' + contact + ' to ' + value);
		
	}, // end function
	
	// Parse messages and chat things. 
	// TODO: Make the chat synchronizing function.
	parseChat: function(data)
	{
		
		
	}, // end function
	
	// Execute the data we got.
	Execute: function(t)
	{
		// Kinda stupid when this is done when the user is logged out.
		// Anyway, we end the session again.
		if (chat.getClsSession().getStatus() == -1)
			return session_end();
		
		// Split the data on new lines.
		var lines = t.split("\n");
		
		// These variables are triggered to true when there are changes. 
		// This will lead to re-rendering of the contacts/chat dialogs.
		var renderContacts = false;
		var renderChat     = false;
		
		var parsing = false;
		
		// Go through all lines..
		for (var i = 0; i < lines.length; i++)
		{
			// A > specifies we're coming to some new element. 
			// For this we need to set parsing of the line itself off,
			// and say we're heading into a new element.
			if (lines[i].substring(0, 1) == '>')
				parsing = false;
				
			// Are we parsing? Well, if so
			if (parsing == true)
			{
				// Parse the line in the right section.
				// Also trigger the render variable to true.
				switch(parsingWhat)
				{
					case 'contacts': 	renderContacts = true; 	this.parseContact(lines[i]); 	break;
					case 'chat': 		renderChat = true; 		this.parseChat(lines[i]); 		break;
					
				} // end switch
				
			} // end if
			else
			{
				// This is a 'heading' line. It specifies what section we're going to.
				parsing = true;
				parsingWhat = lines[i].substring(1); // Don't take the > with it.
				
			} // end else
			
		} // end for
		
		// The contacts list is changed, render it again.
		if (renderContacts == true)
			chat.getChat().getClsContacts().render();
		
		/*
		if (renderChat == true)
			chat.getChat().getClsChat().render();
		*/
		// Reset the loading.
		HideLoading();
		
		// Do a new synchronizing in 3 seconds.
		setTimeout("chat.getChat().getClsLoop().start();", 3000);
		
	} // end function Execute
	
});

// Yay! Library loop loaded.
cimport_loaded('chatbox.Loop');